<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="author" content="wangyupeng">
	<title>个人记名卡发卡</title>
	<script language="javascript" type="text/javascript" src="../script/print.js"></script>
	<script language="javascript" src="../script/common.js" type="text/javascript"></script>
	<script language="javascript" src="../script/card.js" type="text/javascript"></script>
	<script language="javascript" type="text/javascript">
	//页面控件初始化	
	function load(){
		Eui.showNavigation();
		setPages(["readcardview","detview"]);
		CardMgnt.customer.show("customerdiv",null,CardMgnt.PERSONALALL,{isCreate:true});
		CardMgnt.cardUser.show("carduserdiv",null,CardMgnt.PERSONALALL);
		initActiveX();
	}
	//读卡
	function onReadCard(){
		try{
			var data = CardMgnt.findCard(readCardForm)
			CardMgnt.throwIssueErr(data.card,data.cardType,Constants.PERSONAL);
			
			if (data.cardType.userLevel > Constants.CUSTOMERLEVEL_NORMAL)
				throw new Error("卡片类型使用等级高于客户级别，不能出售！");
			$("issueCardbtn").disabled = false;
			
			//缓存手续费
			var fee = CardMgnt.getFee(Constants.ACCOUNT_FEE_SELL_CARD,data.cardType.id);
			var foregift = CardMgnt.getFee(Constants.ACCOUNT_FOREGIFT_CARD,data.cardType.id);
			readCardForm.feeText.value = Format.fenToYuan(fee);
			readCardForm.foregiftText.value = Format.fenToYuan(foregift);
		}catch(e){
			showError(e.description);
		}
	}
	//个人记名卡售卡
	function doIssueCard(){
		var asn = readCardForm.asn.value;
		if (!checkForm(CardMgnt.customer.formName)) return;
		if (!checkForm(CardMgnt.cardUser.formName)) return;
		if (!CardMgnt.checkCardAsn(asn))return;
		if (!showQuestion("是否出售卡片"+asn+"？")) {onCancel();return;}
		
		var customer = CardMgnt.customer.get();
		if (!customer.checkIdno){
			var c = DWR.syncCall("customerService.getCustomerForCardUser",customer.idno,customer.idnoType);
			if (c){
				if (!showQuestion("档案已经存在，是否在该档案下继续创建客户资料？")){onCancel();return;}
				customer.id = c.id
			}
		}
		var cardUser = CardMgnt.cardUser.get();
		cardUser.type = CardUserType.NAME;
		var data;
		try{
			data = DWR.syncCall("cardMgntService.issuePersonalCard",asn,customer,cardUser);
		}catch(e){showError(e.description);return;}
		try{
			showIssueDet(data.logId);
			WebPrint.print("billview");
		}catch(e){
			showError("打印单据出错:"+e.description);
		}
		while(true){
			try{
				//throw new Error("模拟售卡损坏！");
				CardMgnt.syncCardData(asn);
				break;
			}catch(e){
				if (showQuestion("同步卡数据失败！是否重试？<br>错误："+e.description)){
					continue;
				}
				if (showQuestion("售卡产生坏卡，是否换卡？")){
					CardMgnt.badCard.gotoPage(asn,CardMgnt.badCard.PERSONAL);
				}
				return;
			}
		}
		if (data.cardUser.usePin)
			CardMgnt.startCardPin(asn);
		showInformation("售卡成功！");
	}
	
	//显示售卡明细,凭证预览
	function showIssueDet(logId){
		//TODO 凭证预览
		switchPage("detview");
		
		Eui.getBillHtml("billview",{
			queryName:"issue_personal_card_bill",
			logId:logId
		});
	}
	
	//重置页面form
	function onCancel(){
		switchPage("readcardview");
		CardMgnt.resetForm(readCardForm);
		CardMgnt.customer.show("customerdiv",null,CardMgnt.PERSONALALL,{isCreate:true});
		CardMgnt.cardUser.show("carduserdiv",null,CardMgnt.PERSONALALL);
		$("issueCardbtn").disabled = true;
	}
	</script>
	</head>
	<body onload="load()" onunload="releaseCard()">
	<div class="nav"><div id="nav">&nbsp;</div></div>
	
	<div id="readcardview">
		<div id="readcarddiv">
			<form id="readCardForm" name="readCardForm">
				<div class="form">
					<div class="formTitle">个人记名卡售卡</div>
					<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="label6">
						<tr>
							<td><label>卡 号</label> <input type="text" name="asn" readonly class="readonly"></td>
							<td><label>卡类型</label> <input type="text" name="subCardTypeText" readonly class="readonly"></td>
						</tr>
						<tr>
							<td><label>押 金</label> <input type="text" name="foregiftText" readonly class="readonly"></td>
							<td><label>手续费</label> <input type="text" name="feeText" readonly class="readonly"></td>
						</tr>
					</table>
				</div>
			</form>
		</div>
		<div id="customerdiv" class="form">
		</div>
		<div id="carduserdiv" class="form">
		</div>
		<p>
			<button onclick="onReadCard()">读 卡</button>
			<button name="issueCardbtn" id="issueCardbtn" onclick="doIssueCard()" disabled>售 卡</button>
			<button onclick="onCancel()">重 置</button>
		</p>
	</div>
	<!--凭证页面-->
	<div id="detview" class="form" style="width:100%;display:none;">
		<div class="formTitle">
			<div class="l">售卡凭证</div>
			<div class="r">
				<button onclick="WebPrint.preview('billview')">打印预览</button>
		 		<button onclick="WebPrint.print('billview')">打印</button>
		 		<button onclick="onCancel()">返 回</button>
			</div>
		</div>
		<div id="billview" align='center'></div>
	</div>
</body>
</html>